<?php
namespace app\index\controller;

use think\Controller;


class Pay extends controller
{   
	
    public function index(){
      $Recharge  = model('recharge');
      
      $xmlData = file_get_contents('php://input');
      libxml_disable_entity_loader(true);
      $data = json_decode(json_encode(simplexml_load_string($xmlData, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
	    ksort($data);
      
      $buff = '';
      foreach ($data as $k => $v){
          if($k != 'sign'){
              $buff .= $k . '=' . $v . '&';
          }
      }
      $stringSignTemp = $buff . 'key=d57f65ebb69106cd68d1aacd7b8616d7';//key为证书密钥
      $sign = strtoupper(md5($stringSignTemp));
      //判断算出的签名和通知信息的签名是否一致
      if($sign == $data['sign']){
        
        $update_info['start_time'] = time();
        $update_info['is_pay']     = 1;
        
        $Charge_log = db('charge_log');
        //修改订单状态
        $Charge_log->where('out_trade_no',$data['out_trade_no'])->update($update_info);
        if($deposit = $Charge_log->where('out_trade_no',$data['out_trade_no'])->value('deposit')){
            db('user')->where('out_trade_no',$data['out_trade_no'])->update(array('deposit'=>$deposit));

        }
              

        //修改红包状态为使用状态
        $User_redpacket        = db('user_redpacket');
        $redpacket_id = $Charge_log->where('out_trade_no',$data['out_trade_no'])->value('user_redpacket_id');

        $update['status']   = 1;
        $update['use_time'] = time(); 
        $result = $User_redpacket->where('id',$redpacket_id)->update($update);


        echo '<xml>
                <return_code><![CDATA[SUCCESS]]></return_code>
                <return_msg><![CDATA[OK]]></return_msg>
            </xml>';
        exit();

        }
      
	  }


    

}